<?php
/**
 * 获取个人获得的优惠券组件
 */
class CouponWidget extends Widget{
	public function render($data){
		$moduleId = $data['module_id'];
		$userId = $data['user_id'];
		$accountId = $data['account_id'];

		// 查询该时间段的所有卡券
		$coupon = M('coupon_info')->where(array('account_id' => $accountId,'coupon_type' => 0,'start_time' => array('lt',time()),'end_time' => array('gt',time())))->select();
		$couponList = array();
		$couponIdList = array();
		if($coupon){
			foreach($coupon as $v){
				if(in_array($moduleId,json_decode($v['use_module'],true))){
					$couponList[$v['coupon_id']]['title'] = $v['title'];
					$couponList[$v['coupon_id']]['value'] = $v['value'];
					$couponIdList[] = $v['coupon_id'];
				}
			}
		}

		$myCoupon = array();
		if(!empty($couponIdList)){
			// 查询用户领取的卡券
			$record = M('coupon_record')->where(array('user_id' => $userId,'is_use' => 0,'if_send' => 0,'coupon_id' => array('in',$couponIdList)))->select();
			if($record){
				foreach($record as $v){
					$myCoupon[] = array(
						'coupon_title' => $couponList[$v['coupon_id']]['title'],
						'value' => $couponList[$v['coupon_id']]['value'],
						'record_id' => $v['record_id'],
					);
				}
			}
		}

		$data = array(
			'coupon' => $myCoupon,
		);

		$content = $this->renderFile('coupon',$data);
		return $content;
	}
}